package com.stone.activemqcase.config;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 生产者
 */
public class Producer {

    public static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    public static final String ACTIVEMQ_NAME = "queue";

    public static void main(String[] args) throws JMSException {
        //1、创建连接工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2、通过连接工厂获得连接connection并访问
        Connection connection = factory.createConnection();
        connection.start();

        //3、创建会话session
        //第一个参数叫事物第二个叫签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4、创建目的地(具体是主题或者队列)Destination destination可以是主题或者队列
        Queue queue = session.createQueue(ACTIVEMQ_NAME);

        //5、创建生产者
        MessageProducer producer = session.createProducer(queue);

        //6、通过生产者生产三条消息到队列中
        for (int i = 1; i < 4; i++) {
            //7、创建消息
            TextMessage textMessage = session.createTextMessage("msg--------------------->" + i);
            textMessage.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            textMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            //8、生产者发送给mq
            producer.send(textMessage);
        }

        //9、关闭资源
        producer.close();
        session.close();
        connection.close();

        System.out.println("----------------------------------->结束");
    }
}
